<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-22
 * Time: 16:58
 */

namespace App\Model\Mobile;


use App\Http\Controllers\Mobile\Customer\CustomersController;
use App\Model\Backstage\Customers;
use App\Model\Backstage\CustomersFollow;
use App\Model\Backstage\Log;
use App\Model\Backstage\RoomInfo;
use App\Model\Base;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\Eloquent\Model;

class MobilePlan extends Base
{

    public $mobile_plan;

    public function __construct()
    {
        parent::__construct();
        //$this->mobile_plan = 'mobile_plan';//移动端标记表
        $this->mobile_plan = 'mobile_plan'.'_'.$this->branch_id;//移动端标记表
    }
    /**APP 首页查看 当前用户的任务模块
     * @return array|bool
     */
    public function taskModule($hmp_user_id)
    {
        $type =  DB::table($this->mobile_plan)
            ->where('hmp_user_id', $hmp_user_id)
            ->where('hmp_room_customer', 1)
            ->value(DB::raw('GROUP_CONCAT(hmp_sign_type) AS hmp_sign_type'));
        if ($type == null) {
            return [];
        }
        $signType = explode(',', $type);

        $array = array();
        foreach ($signType as $value) {
            if (DB::table($this->mobile_plan)->where('hmp_user_id', $hmp_user_id)->where('hmp_room_customer', 2)->where('hmp_sign_type', $value)->value('hmp_id')){
                array_push($array, $value);
            }
        }
        return $array;

//        $array = array();
//        foreach($signType as $val){
//            if (!in_array($val, $array)) {
//                array_push($array, $val);
//            }
//        }
//        return $array;
    }

    /**计划任务列表
     * @param $uid 用户ID
     * @param $signType 任务类型
     * @return array|bool
     */
    public function listPlan($uid, $signType, $authories)
    {
        $rIdAndCid = DB::table($this->mobile_plan)
            ->where('hmp_user_id', $uid)
            ->where('hmp_sign_type', $signType)
            ->select('hmp_id', 'hmp_room_customer', 'hmp_room_customer_id')
            ->get();
        if (sizeof($rIdAndCid) == 0){
            return false;
        }
        $customers = new Customers();
        $room = new RoomInfo();
        $authories = explode(',', $authories);//该员工拥有的所有权限

        foreach($rIdAndCid as $k=>$v) {
            if ($v->hmp_room_customer == 1) {//客源
//                $v->hmp_room_customer_id  = $customers->customersSameList($v->hmp_room_customer_id);
                $customersdata  = $customers->customersSameList($v->hmp_room_customer_id);
                $v->hmp_room_customer_id = $customers->dealWithData($customersdata, $authories, $uid);

            } else if ($v->hmp_room_customer == 2) {//房源
//                $v->hmp_room_customer_id  = $room->detail($v->hmp_room_customer_id);
                $roomData = $room->detail($v->hmp_room_customer_id);
                $v->hmp_room_customer_id = $room->dealWithData($roomData, $authories, $uid);
            }
        }
        return $rIdAndCid;
    }


    //删除计划任务
    public function delPlan($hmpId){
        return DB::table($this->mobile_plan)
            ->where('hmp_id', $hmpId)
            ->delete();
    }

    //app登录
    public static function appLogin($params)
    {
        $user_info = DB::table('user')->where('u_account_number', $params['u_account_number'])->first();
        if (!$user_info) {
            return -4;//账号不存在
        }
        if ($user_info->u_status == 2) {
            return -5;//账号被禁用
        }
        if ($user_info->is_del == 1 || $user_info->is_del == 2) {
            return -6;//账号被管理者删除
        }
        $u_app_token = $user_info->u_app_token;
        // 判断密码是否正确
        if (Hash::check($params['u_password'], $user_info->u_password)){
            //查询该账号所属的公司状态
            $anmInfo = DB::table('account_number_manager')->where('anm_id', $user_info->u_b_id)->select('anm_status', 'anm_app_login')->first();
            $status  = $anmInfo->anm_status;
            $login = $anmInfo->anm_app_login;
            if ($login != 2) {
                return -7;//该账号不能再APP端登录
            }
            if ($status == 2) {
                //公司还在审核中不能登录
                return -1;
            } elseif ($status == 3) {
                //公司账号到期没有续费无法登录
                return -2;
            } elseif($status == 1) {
                // 生成随机字符串存数据库 用户认证用
                $apptoken = str_random(60);
                $data['u_app_token'] = $apptoken;
                $data['u_last_time'] = time();
                DB::table('user')->where(['u_id'=>$user_info->u_id])->update($data);
                unset($user_info->u_password);
                $log = new Log();
                $log->addlog($user_info->u_d_id, $user_info->u_id, 1, $user_info->u_username.'登录APP',$user_info->u_b_id);
                $res['apptoken'] = $apptoken;//返回TOKEN
                $authoriesArray = explode(',', $user_info->authories);
                $array =[];
                if (sizeof($authoriesArray) != 0) {
                    foreach($authoriesArray as $k=>$v) {
                        $nmtype = explode('|', $v);
                        if ($v) {
                            array_push($array, $nmtype[2]);
                        }
                    }
                }
                $res['authories'] =implode(',', $array);//返回该用户的权限
                $res['bid'] = $user_info->u_b_id;//公司ID
                $res['system_id'] = DB::table('department')->where('c_id', $user_info->u_b_id)->where('d_superior_id', 0)->value('id');//当前公司的系统管理组部门ID
                $res['did'] = $user_info->u_d_id;//所在的部门ID
                $res['uid'] = $user_info->u_id;//员工ID
                $res['username'] = $user_info->u_username;//员工名称
                $dataArray = [$res['bid'],  $res['system_id'], $res['did'], $res['uid'], $res['username']];
                $data = implode(',', $dataArray);
                if ($u_app_token != '') {
                   Redis::del($user_info->u_app_token);
                   Redis::del($user_info->u_app_token.$res['uid']);
                }
                Redis::set($apptoken, $data);//公司ID ,系统管理组部门ID,   部门id ,员工Id,
                Redis::set($apptoken.$res['uid'], $res['authories']);//员工的权限
                return $res;
            }else {
                //未知状态 请联系管理员
                return -3;
            }

        }else{
            return false;
        }

    }

    /**
     * 根据  房源、客源ID   用户ID  房客源的类型  查询该
     */
    public function getSignType($hmp_room_customer, $hmp_room_customer_id, $hmp_user_id)
    {
        return DB::table($this->mobile_plan)
            ->where('hmp_room_customer_id', $hmp_room_customer_id)
            ->where('hmp_room_customer',$hmp_room_customer)
            ->where('hmp_user_id',$hmp_user_id)
            ->value('hmp_sign_type');
    }
}